package com.peng.web;

import com.google.common.base.Joiner;
import org.apache.commons.io.FileUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * HotWordController
 *
 * @author: lupeng6
 * @create: 2023/3/25 21:36
 */
@RestController
@RequestMapping("/api")
public class IkHotWordController {

    @RequestMapping("/hotWord")
    public void hotWord(Integer wordlib, HttpServletResponse response) throws IOException {
        File file = null;
        if (wordlib == 1) {
            file = ResourceUtils.getFile("classpath:es/ik_entand.dic");
        } else {
            file = ResourceUtils.getFile("classpath:es/ik_stopword.dic");
        }
        response.setContentType("text/plain;charset=utf-8");
        List<String> words = FileUtils.readLines(file, "UTF-8");
        response.setHeader("Last-Modified", String.valueOf(words.size()));
        response.setHeader("ETag", String.valueOf(words.size()));
        ServletOutputStream out = response.getOutputStream();
        String finalStr = Joiner.on("\n").join(words);
        out.write(finalStr.getBytes(StandardCharsets.UTF_8));
        out.flush();
    }
}
